<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
     <el-form-item label="报名账号" prop="sportNameUser">
        <el-input
          v-model="queryParams.sportNameUser"
          placeholder="请输入项目主题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="项目名称" prop="declareXmName">
        <el-input
          v-model="queryParams.declareXmName"
          placeholder="请输入项目名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="项目主题" prop="sportXmTheme">
        <el-input
          v-model="queryParams.sportXmTheme"
          placeholder="请输入项目主题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['sportDeclareInformation:declareInformation:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['sportDeclareInformation:declareInformation:remove']"
          >取消报名</el-button
        >
      </el-col>
       <el-col :span="1.5">
        <el-button
            type="info"
              plain
              icon="el-icon-upload2"
              size="mini"
              @click="handleImport"
          v-hasPermi="['sportDeclareInformation:declareInformation:import']"
          >导入成绩</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['sportDeclareInformation:declareInformation:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
     :row-class-name="tableRowClassName"
      v-loading="loading"
      :data="declareInformationList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="报名id" align="center" prop="declareUid" width="90px"/>
      <!-- <el-table-column label="项目id" align="center" prop="sportNameAccount" /> -->
      <el-table-column
        label="当前报名账号"
        align="center"
        prop="sportNameUser"
        width="180px"
      />
      <el-table-column label="运动会届目" align="center" prop="sportDename" />
      <el-table-column label="项目名称" align="center" prop="declareXmName" />
      <el-table-column label="项目主题" align="center" prop="sportXmTheme" />

      <!-- <el-table-column label="我的成绩" align="center" prop="sportGrade" />
      <el-table-column
        label="我的成绩备注1"
        align="center"
        prop="sportGradeA"
      />
      <el-table-column label="我的获奖" align="center" prop="sportPalm" />
      <el-table-column label="我的获奖备注1" align="center" prop="sportPalmA" /> -->
      <!-- <el-table-column label="备注" align="center" prop="remark" /> -->
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['sportDeclareInformation:declareInformation:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['sportDeclareInformation:declareInformation:remove']"
            >取消报名</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改学生报名信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="109px">
        <!-- <el-form-item label="运动会届数id" prop="sportListId">
          <el-input v-model="form.sportListId" placeholder="请输入运动会届数id" />
        </el-form-item> -->
        <!-- <el-form-item label="报名/登录账号" prop="sportNameAccount">
          <el-input
            v-model="form.sportNameAccount"
            placeholder="请输入报名/登录账号"
          />
        </el-form-item> -->

        <el-form-item label="当前报名姓名" prop="sportNameUser">
          <el-input
            v-model="form.sportNameUser"
            placeholder="请输入当前报名姓名"
          />
        </el-form-item>
        <el-form-item label="运动会届" prop="sportDename">
          <el-input
            v-model="form.sportDename"
            placeholder="请输入当前运动会届"
          />
        </el-form-item>
        <el-form-item label="项目名称" prop="declareXmName">
          <el-input v-model="form.declareXmName" placeholder="请输入项目名称" />
        </el-form-item>
        <el-form-item label="项目主题" prop="sportXmTheme">
          <el-input v-model="form.sportXmTheme" placeholder="请输入项目主题" />
        </el-form-item>
        <el-form-item label="我的成绩" prop="sportGrade">
          <el-input v-model="form.sportGrade" placeholder="请输入项目主题" />
        </el-form-item>
        <el-form-item label="获得积分" prop="sportGradeA">
          <el-input v-model="form.sportGradeA" placeholder="请输入项目主题"/>
        </el-form-item>
        <el-form-item label="我的获奖" prop="sportPalm">
          <el-input v-model="form.sportPalm" placeholder="请输入项目主题" />
        </el-form-item>
        <el-form-item label="我的获奖备注1" prop="sportPalmA">
          <el-input v-model="form.sportPalmA" placeholder="请输入项目主题" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


    <!-- 学生成绩导入 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="500px" append-to-body>
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <div class="el-upload__tip" slot="tip">
            <el-checkbox v-model="upload.updateSupport" /> 是否更新已经存在的用户数据
          </div>
          <span>仅允许导入xls、xlsx格式文件。</span>
          <!-- <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;" @click="importTemplate">下载模板</el-link> -->
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>
<style>
  .el-table .warning-row {
    background: oldlace;
  }

  .el-table .success-row {
    background: #f0f9eb;
  }
</style>
<script>
import {
  listDeclareInformation,
  getDeclareInformation,
  delDeclareInformation,
  addDeclareInformation,
  updateDeclareInformation,
} from "@/api/sportDeclareInformation/declareInformation";
import { getToken } from "@/utils/auth";

export default {
  name: "DeclareInformation",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 学生报名信息表格数据
      declareInformationList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,

     // 学生导入参数
      upload: {
        // 是否显示弹出层（学生成绩数据导入）
        open: false,
        // 弹出层标题（学生成绩数据导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的学生成绩数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/sportDeclareInformation/declareInformation/importData"
      },

      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        sportNameAccount: null,
        sportNameUser: null,
        declareXmName: null,
        sportXmTheme: null,
        sportName: null,
        sportDename: null, //运动会届数
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    //css样式
    tableRowClassName({row, rowIndex}) {
        if (rowIndex %2== 1) {
          return 'warning-row';
        } else if (rowIndex %2== 0) {
          return 'success-row';
        }
        return '';
      },
    /** 查询学生报名信息列表 */
    getList() {
      this.loading = true;
      listDeclareInformation(this.queryParams).then((response) => {
        this.declareInformationList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        declareUid: null,
        sportListId: null,
        sportNameAccount: null,
        sportNameUser: null,
        declareXmName: null,
        sportXmTheme: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        sportDename: null,
        sportGrade:null,
        sportGradeA:null,
        sportPalm:null,
        sportPalmA:null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.declareUid);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加学生报名信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      console.log(row)
      this.reset();
      const declareUid = row.declareUid || this.ids;
      getDeclareInformation(declareUid).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改学生报名信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.declareUid != null) {
            updateDeclareInformation(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDeclareInformation(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const declareUids = row.declareUid || this.ids;
      this.$modal
        .confirm(
          '是否确认删除已报项目为："' + row.declareXmName + '"的数据项？'
        )
        .then(function () {
          return delDeclareInformation(declareUids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },

     /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "成绩导入";
      this.upload.open = true;
    },

   // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert(response.msg, "导入结果", { dangerouslyUseHTMLString: true });
      this.getList();
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    },

    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "sportDeclareInformation/declareInformation/export",
        {
          ...this.queryParams,
        },
        `declareInformation_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>


